const commentService = require("../service/comment.service");

class CommentController {
	// 评论
	async create(ctx, next) {
		// 获取参数 (user_id, moment_id, comment_id(默认为空), content)
		const { moment_id, content } = ctx.request.body;
		const user_id = ctx.user.id;
		// 发表评论
		const result = await commentService.create(user_id, moment_id, content);
		// 返回数据
		ctx.body = {
			code: 200,
			message: "发表评论成功",
			data: result
		};
	}

	// 回复评论
	async reply(ctx, next) {
		// 获取参数 (user_id, moment_id, comment_id, content)
		const user_id = ctx.user.id;
		const { moment_id, content } = ctx.request.body;
		const comment_id = ctx.params.commentId;
		// 回复评论
		const result = await commentService.reply(
			user_id,
			moment_id,
			comment_id,
			content
		);
		// 返回数据
		ctx.body = {
			code: 200,
			message: "回复评论成功",
			data: result
		};
	}

	// 修改评论
	async update(ctx, next) {
		// 获取参数 (comment_id, content)
		const { content } = ctx.request.body;
		const comment_id = ctx.params.commentId;
		// 修改评论
		const result = await commentService.update(comment_id, content);
		// 返回数据
		ctx.body = {
			code: 200,
			message: "修改评论成功",
			data: result
		};
	}

	// 删除评论
	async remove(ctx, next) {
		const { commentId } = ctx.params;
		const result = await commentService.remove(commentId);
		// 返回数据
		ctx.body = {
			code: 200,
			message: "删除评论成功",
			data: result
		};
	}

	// 获取评论列表
	async list(ctx, next) {
		const { momentId } = ctx.query;
		const result = await commentService.getCommentById(momentId);
		// 返回数据
		ctx.body = result;
	}
}

module.exports = new CommentController();
